<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@tag import="com.cb.domain.service.bbase.CategoryManager"%>
<%@tag import="com.cb.core.util.AppUtil"%>
<%@tag import="java.util.List"%>
<%@tag import="org.apache.commons.lang.StringUtils"%>
<%@ attribute name="multiple" description=" allowed multiple selected,default value is 'multiple'"%>
<%@ attribute name="callback" description="function will call when the item changeed "%>
<%@ attribute name="cssClass" description="the class of style for the select control"%>
<%@ attribute name="size" description="the size for the select show"%>
<%@ attribute name="ondbclick" description="the event for the double click"%>
<%@ attribute name="selectedCatId" description="selected category id"%>

<%@tag import="com.cb.domain.model.bbase.Category"%>
<%@tag import="java.util.Map"%>
<%@tag import="java.util.HashMap"%>
	
<table>
	<tr>
		<td valign="top">
			<select name="cat1" id="cat1"  onChange="fillNextChildCategoryFree(1);${callback}" ondblclick="${ondbclick}" size="${size}" level="1" ${multiple} class='${cssClass}'>
			</select>
		</td>
		<td valign="top">
			<select name="cat2" id="cat2"  onChange="fillNextChildCategoryFree(2);${callback}" ondblclick="${ondbclick}" size="${size}"  level="2" ${multiple} class='${cssClass}'>
			</select>
		</td>
		<td valign="top">
			<select name="cat3" id="cat3"  onChange="fillNextChildCategoryFree(3);${callback}" ondblclick="${ondbclick}" size="${size}" level="3" ${multiple} class='${cssClass}'>
			</select>
		</td>
		<td valign="top">
			<select name="cat4" id="cat4" level="4" ${multiple} size="${size}" ondblclick="${ondbclick}" class='${cssClass}'>
			</select>
		</td>
	</tr>
</table>
<script type="text/javascript">
loadAndInitCategoryFree();
fillFirstCategoryFree();
</script>
<c:set var="selectedCatId" value="${selectedCatId}" scope="request"/>
	
<%
	/**
	*  select category if ${selectedCatId} had been specific
	**/
	String selectedCatId = (String)request.getAttribute("selectedCatId");
	if (StringUtils.isNotBlank(selectedCatId)) {
		CategoryManager categoryManager=(CategoryManager)AppUtil.getById("categoryFreeManager");
		Category category = categoryManager.get(new Long(selectedCatId));
		Map map = new HashMap();
		int totalLevel = 1;
		Short i = 1;
		while (category.getParentCategory() != null) {
			Category parentCategory = category.getParentCategory();
			map.put(category.getCategoryLevel(),category.getCategoryId());
			category = parentCategory;
			i++;
		}
		map.put(category.getCategoryLevel(),category.getCategoryId());
		
		for (Short j=1; j<=i; j++) {

%>
	<script type="text/javascript">
	selectCategoryFree(<%=j%>,<%=map.get(j)%>);
	fillNextChildCategoryFree(<%=j%>);
	</script>
<%
		}
	}
%>
